package com.thinkgem.jeesite.modules.magnetic.util;

import com.drew.lang.BufferReader;
import org.springframework.stereotype.Component;

import java.io.*;
import java.net.Socket;


/**
 * @author xlp
 * @date 2019/5/5
 * 信号灯控制器
 */
@Component
public class SignLightControl {
    //继电器IP地址和端口
    private final int PORT = 10000;
    private final String ADDRESS = "192.168.1.232";
    //打开绿灯命令
    public final String OPEN_GREEN = "FE 05 00 00 FF 00 98 35";
    //关闭绿灯命令
    public final String OFF_GREEN = "FE 05 00 00 00 00 D9 C5";
    //打开红灯命令
    public final String OPEN_RED = "FE 05 00 01 FF 00 C9 F5";
    //关闭红灯命令
    public final String OFF_RED = "FE 05 00 01 00 00 88 05";
    //打开全部信号灯命令
    public final String OPEN_ALL = "FE 0F 00 00 00 02 01 FF D1 D3";
    //关闭全部信号灯命令
    public final String OFF_ALL = "FE 0F 00 00 00 02 01 00 91 93";

    public void controlSighLight(String cmd) {
        Socket socket = null;
        try {
            socket = new Socket(ADDRESS, PORT);
            // 创建一个写线程
            OutputStream os = socket.getOutputStream();
            os.write(TCPClient.hexStrToBinaryStr(cmd));
            os.flush();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            socket.shutdownOutput();
            InputStream is = socket.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            DataInputStream dis = new DataInputStream(bis);
            byte[] b = new byte[1];     //一次只读取一个byte
            String ret = "";
            while (dis.read(b)!=-1){
                ret += TCPClient.bytesToHexString(b) + " ";
                if (dis.available() == 0) { //一个请求
                    System.out.println( "捕获的信息:" + ret.toUpperCase());
                    ret = "";
                }
            }
            os.close();
            socket.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    class TelnetWriter extends Thread{
        private PrintStream out;

        public TelnetWriter(OutputStream out) {
            this.out = new PrintStream(out);
        }
        public void run() {
            try {
                // 包装控制台输入流
                BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
                // 反复将控制台输入写到Telnet服务程序
                while (true)
                    out.println(in.readLine());
            } catch (IOException exc) {
                exc.printStackTrace();
            }
        }
    }

    class TelnetReader extends Thread {
        private InputStreamReader in;

        public TelnetReader(InputStream in) {
            this.in = new InputStreamReader(in);
        }
        public void run() {
            try {
                // 反复将Telnet服务程序的反馈信息显示在控制台屏幕上
                while (true) {
                    // 从Telnet服务程序读取数据
                    int b = in.read();
                    if (b == -1)
                        System.exit(0);
                    // 将数据显示在控制台屏幕上
                    System.out.print((char) b);
                }
            } catch (IOException exc) {
                exc.printStackTrace();
            }
        }
    }

}
